import { _decorator, Button, Component, find, log, Node, Sprite, SpriteFrame } from 'cc';
import { BundleManager, Bundles } from '../../AshFramework/Managers/BundleManager';
import { GameManager } from '../GameManager';
import { Money } from '../Money';
import { EventManager, MyEvent } from '../../AshFramework/Managers/EventManager';
import { TASKTYPE, weaponPrice } from '../../AshFramework/Datas/GameConstant';
import { GameUtil } from '../GameUtil';
const { ccclass, property } = _decorator;

@ccclass('UICarriage')
export class UICarriage extends Component {
    @property
    type = 0;

    private _isLockNode: Node = null;
    private _buyNode: Node = null;
    private _weaponSprite: Sprite = null;

    checkedNode: Node = null;

    get isLock(): boolean {
        return this._isLockNode.active;
    }

    protected onLoad(): void {
        this._isLockNode = find("isLock", this.node);
        this._buyNode = find("isLock/按钮", this.node);
        this._weaponSprite = find("武器", this.node).getComponent(Sprite);
        this.checkedNode = find("isChecked", this.node);
    }

    protected start(): void {

    }

    click() {
        let money = this._buyNode.getComponent(Money).money;
        if (GameManager.Instance.userData.gold < money) {
            GameUtil.showHint("金币不够");
            return;
        }

        GameManager.Instance.userData.gold -= money;
        GameManager.Instance.userData.weapon[this.type - 1] = this.type;
        EventManager.Scene.emit(MyEvent.CARRIAGE_BUY);
        this.unlock();
        //任务
        if (GameManager.Instance.userData.task.type === TASKTYPE.CONSUME) {
            GameManager.Instance.userData.task.curProgress += money;
        }
    }

    unlock() {
        this._isLockNode.active = false;
    }

    protected onEnable(): void {
        this._buyNode.on("click", this.click, this);

        let epoch = GameManager.Instance.userData.epoch;
        let path = `${epoch}武器${this.type}`;
        BundleManager.LoadSpriteFrame(Bundles.Sprites, path).then((sf: SpriteFrame) => {
            this._weaponSprite.spriteFrame = sf;
        })

        let price = weaponPrice[epoch][this.type - 1];
        this._buyNode.getComponent(Money).setMoney(price);
    }

    protected onDisable(): void {
        this._buyNode.off("click", this.click, this);
    }

}


